/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.hauntedhouses.service.impl;

import cz.muni.fi.hauntedhouses.dao.HumanDAO;
import cz.muni.fi.hauntedhouses.dao.UsersDAO;
import cz.muni.fi.hauntedhouses.service.UsersService;
import cz.muni.fi.hauntedhouses.to.HumanTO;
import cz.muni.fi.hauntedhouses.to.UsersTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author honzaq
 */
@Service
@Transactional

public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersDAO userDao;
    
    private ShaPasswordEncoder passwordEncoder;

    public void setPasswordEncoder(ShaPasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
    
    @Override
    public void create(UsersTO user) {
            userDao.create(user); //There’s a reason for saving the account before actually hashing the password.
            
            String encPassword = passwordEncoder.encodePassword(user.getPassword(), null);
            user.setPassword(encPassword);
            userDao.update(user);
    }

    @Override
    public UsersTO get(Long id) {
        return userDao.get(id);
    }

    @Override
    @Secured({"ROLE_ADMIN"})
    public void remove(UsersTO user) {
        userDao.delete(user);
    }

    @Override
    @Secured({"ROLE_ADMIN"})
    public void update(UsersTO user) {
        userDao.update(user);
    }
}
